package com.acc.controller;

import java.io.IOException;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.acc.entity.Msg;
import com.acc.entity.User;
import com.acc.service.UserService;
import com.acc.service.impl.UserServiceImpl;
import com.alibaba.fastjson.JSON;

/**
 * 
 * @author 姜伟康 2017-9-15
 */

public class UserController extends InnerDispacherController {

	UserService userService = new UserServiceImpl();
	
	boolean result = false ;
	
	

	// 根据用户ID查询用户
	public void selectUserById(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException,
			SQLException {

		//从session中获取用户信息
        HttpSession httpSession = request.getSession();
        User user = (User) httpSession.getAttribute( "user");
		// 转发到成功提示界面
		response.setContentType("application/json");
		response.setCharacterEncoding("utf-8");
		List<User> users = new ArrayList<User>();
		users.add(user);
		response.getWriter().append(JSON.toJSONString(users));
	}

	// 修改用户信息
	public void updateUser(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException,
			SQLException {

		// 获取要修改的用户信息
		User user = new User();

		user.setGender(Integer.parseInt(request.getParameter("gender")));
		user.setIdcard(request.getParameter("idcard"));
		user.setPassword(request.getParameter("password"));
		user.setType(Integer.parseInt(request.getParameter("type")));
		user.setUserName(request.getParameter("userName"));
		user.setUserId(Integer.parseInt(request.getParameter("userId")));
		// 执行修改
		result = userService.updateUser(user);
		// 判断是否成功
		if (result) {
			Msg msg = new Msg();
			msg.setSuccess(true);
			msg.setErrorMsg(null);
			response.getWriter().append(JSON.toJSONString(msg));
			
		}

	}

}
